package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.dialog.CommDialog;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.my.adapter.DepartmentAdapter;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.model.DepartmentModel;
import com.jp.knowledge.my.resources.OrganizeJoinRes;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.RecycleViewDiver;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

public class DepartmentActivity extends SlidingActivity implements ResultSubscriber.OnResultListener, RecycleVAdapter.OnItemClickListener, View.OnClickListener {

    @ViewInject(R.id.department_show_rv)
    private RecyclerView searchList;
    @ViewInject(R.id.department_btn_create)
    protected TextView departmentCreateBn;

    protected int requestCode;
    protected DepartmentAdapter adapter;


    private UserData userData;
    private CommDialog deleteDialog;
    private int deletePosition;

    @Override
    protected int getResourceId() {
        return R.layout.activity_department;
    }

    @Override
    protected void init() {

        this.requestCode = getIntent().getIntExtra(OrganizeJoinRes.REQUEST_CODE, 0);
        initTitle(getIntent().getStringExtra(OrganizeJoinRes.TITLE));

//        if(this.requestCode == OrganizeJoinRes.SCHOOL_INDEX)
            departmentCreateBn.setVisibility(View.GONE);

        adapter = new DepartmentAdapter(this, new ArrayList<DepartmentModel>());
        adapter.addItemClickListener(this);
        searchList.setHasFixedSize(true);
        searchList.setLayoutManager(new LinearLayoutManager(this));
        searchList.addItemDecoration(new RecycleViewDiver(this, LinearLayout.HORIZONTAL));
        searchList.setAdapter(adapter);
        leftIcon.setOnClickListener(this);
        departmentCreateBn.setOnClickListener(this);
        getData();

    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK || requestCode != this.requestCode || null == data)
            return;
        getData();
    }

    private void initTitle(String title) {
        topName.setText(title);
        topDesc.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        rightIcon.setVisibility(View.GONE);
    }

    @Override
    public void onStart(int requestType) {
        loading.show();
    }
    @Override
    public void onCompleted(int requestType) {
        loading.cancel();
    }
    @Override
    public void onError(int requestType) {
        loading.cancel();
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(requestType != this.requestCode)
            return;
        adapter.updateAdapter(data.getList(DepartmentModel.class));
    }

    @Override
    public void itemSelect(int position) {
        Intent intent = getIntent();
        intent.putExtra(OrganizeJoinRes.RESPONSE_RESULT, adapter.getData(position).getName());
        intent.putExtra(OrganizeJoinRes.DEPA_ID, adapter.getData(position).getId());
        setResult(RESULT_OK, intent);
//        finish();
        if(requestCode == OrganizeJoinRes.SCHOOL_INDEX)
            intent.setClass(this, ClassActivity.class);
        else
            intent.setClass(this, PositionActivity.class);
        startActivity(intent);
    }

    protected void getData(){
        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
        param.addProperty("id", getIntent().getStringExtra(OrganizeJoinRes.ID));
        param.addProperty("type", requestCode+1);
        HttpHelper.getInstance(this).getDepartment(param, requestCode, this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == leftIcon.getId()){
            finish();
        } else if(v.getId() == R.id.department_btn_create){
            createBnClick();
        }
    }

    protected void createBnClick(){
        Intent intent = getIntent();
        intent.setClass(this, DepartmentCreateActivity.class);
        intent.putExtra(OrganizeJoinRes.TITLE_CREATE, getString(R.string.create_department_title));
        intent.putExtra(OrganizeJoinRes.REMIND_CREATE, getString(R.string.create_department_remind));
        startActivityForResult(intent, requestCode);
    }
}
